<?php
require_once APPLICATION_PATH . "/models/ModelBase.php";

class Admins extends TT_Table_Abstract
{
     protected $_name = 'users';
     protected $_primary = 'uid';
      
       
        public function new_user($name,$pass)                         //tao user moi
            {
   
              $db = $this->getSessionDB(); 
              $sql="INSERT INTO users (`name`,`pass` )
                                            VALUES ( ?, ?)";           
              return $db->query($sql,array($name,$pass));
            }
            
            
              public function new_user_info($name, $birthday,$hometown,$address,$pic,$phong_ban,$chuc_danh,$tamtay_acc,$yahoo_acc,$tamtay_email,$date_join,$date_out,$status,$user_info,$phone,$contact)                         //thong tin user moi
            {
             if($pic=="")
                {
                    $pic='/images/nopic.jpg';
                }   
                     $time=time();
              $db = $this->getSessionDB(); 
              $sql=" INSERT INTO users_info (`name`, `birthday`, `hometown`, `address`, `pic`, `phong_ban`, `chuc_danh`, `tamtay_acc`, `yahoo_acc`, `tamtay_email`, `date_join`, `date_out`, `status`, `user_info`, `lastvisited`, `updated`, `phone`,`contact`)
                       VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '0', $time, ?,?)
                    ";           
              return $db->query($sql,array($name,$birthday,$hometown,$address,$pic,$phong_ban,$chuc_danh,$tamtay_acc,$yahoo_acc,$tamtay_email,$date_join,$date_out,$status,$user_info,$phone,$contact));
            }
 
 
 
        public function checkName($name)  //ham kiem tra ten co trung` ko
            {
               $db = $this->getSessionDB(); 
               $sql="SELECT uid FROM users WHERE name=?";
                $a=$db->fetchRow($sql, array($name));
                $b=true;
                if(isset($a->uid))
                    {$b=false;}
               
               return $b;
            }
        public function checkName1($uid) //lay ten dang nhap tu uid
            {
               $db = $this->getSessionDB(); 
               $sql="SELECT name FROM users WHERE uid=?";
               return $db->fetchRow($sql, array($uid)); 
            }
        public function addmoderator($uid,$name,$value)           //value =1 admin 2 mod chung 3 vaytion 4 nhan vien 5 bao cao
            {
                $db=$this->getSessionDB();
                $sql="INSERT INTO manage (`uid` , `manage`,`name`) VALUES (? ,?, ?)";
                return $db->query($sql,array($uid,$value,$name));
            }
        public function removemoderator($user)
            {
                $db=$this->getSessionDB();
                $sql="DELETE FROM manage WHERE id=?";
                return $db->query($sql,array($user));
            }
            
         public function checkInfo($username)
            {
               $db = $this->getSessionDB();
                $sql = 'SELECT * FROM users WHERE name=? ';
                return $db->fetchRow($sql, array($username));         
            }
         public function moderator()     // cho ra ten cac moderator  
            {
                $db = $this->getSessionDB();
                $sql="SELECT id,name,manage FROM `manage`where manage !=1";
                $array=array();
                $a=$db->fetchAll($sql);
                
                return $a;
            }
         public function checkmoderator($uid,$manage)
            {
                $db = $this->getSessionDB();
                $sql="SELECT * FROM manage where uid=? and (manage=2 or manage=?)";
                return $db->fetchAll($sql,array($uid,$manage));
            }
            
         public function deleteuser($username)
            {
                $db=$this->getSessionDB();
                $sql="DELETE FROM users WHERE `users`.`name` = ?;";
                return $db->query($sql,array($username));   
            }
            
         public function deleteuser_info($uid)
            {
                $db=$this->getSessionDB();
                $sql="DELETE FROM users_info WHERE uid = ?";
                return $db->query($sql,array($uid));   
            }
         
         public function banned($user)
            {
                $db=$this->getSessionDB();
                $sql="UPDATE users SET `banned` = '1' WHERE `users`.`name` =?";
                return $db->query($sql,array($user));       
            }
            
         public function unbanned($user)
            {
                $db=$this->getSessionDB();
                $sql="UPDATE users SET `banned` = '0' WHERE `users`.`name` =?";
                return $db->query($sql,array($user));
                
            }
         
       
         
         public function edit($name,$birthday,$hometown,$address,$phone,$pic,$phong_ban,$chuc_danh,$tamtay_acc,$yahoo_acc,$tamtay_email,$date_join,$date_out,$status,$user_info,$uid,$contact)
            {
               $t=time();
                $db=$this->getSessionDB();
                $sql="UPDATE users_info SET  name=? ,birthday=? , hometown=? ,address=? ,phone=?,pic=?,phong_ban=? ,chuc_danh=? ,tamtay_acc=? ,yahoo_acc=? ,tamtay_email=? ,date_join=? ,date_out=? ,status=? , user_info=? ,updated=$t,contact=? WHERE uid =?";
                return $db->query($sql,array($name,$birthday,$hometown,$address,$phone,$pic,$phong_ban,$chuc_danh,$tamtay_acc,$yahoo_acc,$tamtay_email,$date_join,$date_out,$status,$user_info,$contact,$uid));
            }
         
         public function banned_user()    // dua ra nhung user bi ban
            {
                $db = $this->getSessionDB();
                $sql="SELECT name FROM `users`where banned =1";
                $array=array();
                $a=$db->fetchAll($sql);
                
                return $a;
            }
          
      
         public function info($uid) //lay thong tin cua user co uid=uid
               {
               $db = $this->getSessionDB();
               $sql = 'SELECT * FROM users_info WHERE uid=? ';
               return $db->fetchRow($sql, array($uid));         
              }
              
         public function chuc_danh()
            {
                $db = $this->getSessionDB();
               $sql = 'SELECT chuc_danh FROM chuc_danh ';
               return $db->fetchAll($sql);         
            }
            public function them_chuc_danh($t)
                {
                    $db = $this->getSessionDB();
                    $sql = "INSERT INTO chuc_danh (`chuc_danh`) VALUES (?)";
                    return $db->query($sql,array($t));
                }   
                
            public function xoa_chuc_danh($t)
                {
                    $db = $this->getSessionDB();
                    $sql = "DELETE FROM chuc_danh WHERE `chuc_danh`.`chuc_danh` =? ";
                    return $db->query($sql,array($t));         
                }
            public function phong_ban()
            {
                $db = $this->getSessionDB();
               $sql = 'SELECT * FROM phong_ban ';
               return $db->fetchAll($sql);         
            }
            
           public function them_phong_ban($phong_ban)
                {
                    $db = $this->getSessionDB();
                    $sql = "INSERT INTO phong_ban (`phong_ban`,`id_manager`) VALUES (?,1)";
                    return $db->query($sql,array($phong_ban));
                }
           public function xoa_phong_ban($t)
                {
                    $db = $this->getSessionDB();
                    $sql = "DELETE FROM phong_ban WHERE `phong_ban`.`phong_ban` =? ";
                    return $db->query($sql,array($t));
                } 
           
            public function trang_thai()
            {
                $db = $this->getSessionDB();
               $sql = 'SELECT trang_thai FROM trang_thai';
               return $db->fetchAll($sql);         
            }
            
           public function them_trang_thai($t)
                {
                    $db = $this->getSessionDB();
                    $sql = "INSERT INTO trang_thai (`trang_thai`) VALUES (?)";
                    return $db->query($sql,array($t));
                }  
                
           public function xoa_trang_thai($t)
                {
                    $db = $this->getSessionDB();
                    $sql = "DELETE FROM trang_thai WHERE `trang_thai`.`trang_thai` =? ";
                    return $db->query($sql,array($t));
                }                      
}
